﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using DevExpress.XtraEditors;
using Manager.FormAdd_Edit;
using Manager.App_Code;
using Manager.Common;
using Manager.ViewModels;
using System.Data.Linq;

namespace Manager.FormMain
{
    public partial class Frm_BarButton_SapXepPhongDatTruoc : DevExpress.XtraEditors.XtraForm
    {
        NewPearl_HotelDataContext db = new NewPearl_HotelDataContext();

        public Frm_BarButton_SapXepPhongDatTruoc()
        {
            InitializeComponent();
        }

        private void Frm_BarButton_SapXepPhongDatTruoc_Load(object sender, EventArgs e)
        {
            db.Refresh(RefreshMode.OverwriteCurrentValues, db.PhieuDatPhongs);
            gridListBooking.DataSource = db.PhieuDatPhongs.ToList();
            gridDetailRoom.DataSource = null;
        }

        private void gridListBooking_Click(object sender, EventArgs e)
        {
            int index = gridView_ListBooking.FocusedRowHandle;
            var maphieudat = gridView_ListBooking.GetRowCellValue(index, MaPhieuDat).ToString();
            var chiTiet_ChiTietPhieuDat = (from pdp in db.PhieuDatPhongs
                                           join ctpdt in db.ChiTietPhieuDatPhongs on pdp.MaPhieuDat equals ctpdt.MaPhieuDat
                                           join ct_ct in db.ChiTiet_ChiTietPhieuDatPhongs on ctpdt.ChiTietPhieuDatPhongId equals ct_ct.ChiTietPhieuDatPhongId
                                           join lp in db.LoaiPhongs on ctpdt.MaLoaiPhong equals lp.MaLoaiPhong
                                           where pdp.MaPhieuDat == maphieudat
                                           select new { ct_ct.Id,
                                                        ct_ct.ChiTietPhieuDatPhongId,
                                                        ct_ct.MaPhong,
                                                        ct_ct.TrangThai,
                                                        ctpdt.MaLoaiPhong,
                                                        lp.TenLoaiPhong,
                                                        lp.GiaPhong,
                                                        pdp.TenKhach
                                           }).ToList();
            if (chiTiet_ChiTietPhieuDat.Count > 0)
            {
                gridDetailRoom.DataSource = chiTiet_ChiTietPhieuDat;
            }
        }

        private void gridDetailRoom_DoubleClick(object sender, EventArgs e)
        {
            int index = gridView_DetailRoom.FocusedRowHandle;
            var machitiet = gridView_DetailRoom.GetRowCellValue(index, MaChiTiet).ToString();
            var tenLoaiPhong = gridView_DetailRoom.GetRowCellValue(index, TenLoaiPhong).ToString();
            var donGia = gridView_DetailRoom.GetRowCellValue(index, DonGia).ToString();
            var tenKhach = gridView_DetailRoom.GetRowCellValue(index, TenKhach).ToString();
            var temp = db.ChiTiet_ChiTietPhieuDatPhongs.Where(i => i.Id == long.Parse(machitiet)).ToList();
            if (temp.Count > 0)
            {
                Frm_SapXepPhong frmsapphong = new Frm_SapXepPhong(temp.First(), tenLoaiPhong, CommonFunction.ParseDouble(donGia), tenKhach);
                frmsapphong.ShowDialog();
                gridListBooking_Click(null, null);
            }
        }

        private void butRefresh_Click(object sender, EventArgs e)
        {
            Frm_BarButton_SapXepPhongDatTruoc_Load(sender, e);
        }

        private void btnTim_Click(object sender, EventArgs e)
        {           
            try
            {
                var lstPhieuDat = db.PhieuDatPhongs.Where(i => i.NgayDen == dateTuNgay.DateTime && i.NgayDi == dateDenNgay.DateTime).ToList();
                gridListBooking.DataSource = lstPhieuDat;
                
            }
            catch (Exception ex)
            {
                Log.WriteLog("Frm_BarButton_SapXepPhongDatTruoc:btnTim_Click " + ex.Message + "\n" + ex.StackTrace);
                MessageBox.Show("Vui lòng xem lại dữ liệu nhập!");
            }
        }

        private void butThemMoi_Click(object sender, EventArgs e)
        {
            frm_RegistryRoom regis = new frm_RegistryRoom(null);
            regis.ShowDialog();
        }

        private void butHieuChinh_Click(object sender, EventArgs e)
        {
            try
            {
                int index = gridView_ListBooking.FocusedRowHandle;
                var phieuDatPhong = gridView_ListBooking.GetRow(index) as PhieuDatPhong;
                if (phieuDatPhong.ChiTietPhieuDatPhongs[0].ChiTiet_ChiTietPhieuDatPhongs[0].MaPhong == "")
                {
                    frm_RegistryRoom regis = new frm_RegistryRoom(phieuDatPhong);
                    regis.ShowDialog();
                }
                else
                {
                    MessageBox.Show("Bạn đã chọn phòng rồi\n không thể chỉnh sửa phiếu đặt phòng được!!!");
                }
            }
            catch (Exception ex)
            {
                Log.WriteLog("Frm_BarButton_SapXepPhongDatTruoc:butHieuChinh_Click " + ex.Message + "\n" + ex.StackTrace);
                MessageBox.Show("Có lỗi xảy ra!!!");
            }
        }

        private void butHuy_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dr = MessageBox.Show("Bạn có muốn xóa không?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.OK)
                {
                    int index = gridView_ListBooking.FocusedRowHandle;
                    var phieuDatPhong = gridView_ListBooking.GetRow(index) as PhieuDatPhong;
                    var lstChiTietPhieuDat = phieuDatPhong.ChiTietPhieuDatPhongs.ToList();
                    foreach (var ctpd in lstChiTietPhieuDat)
                    {
                        foreach (var ct_ct in ctpd.ChiTiet_ChiTietPhieuDatPhongs.ToList())
                        {
                            if (!string.IsNullOrEmpty(ct_ct.MaPhong))
                            {
                                var phong = db.Phongs.Single(i => i.MaPhong == ct_ct.MaPhong);
                                phong.TinhTrang = ((int)CommonEnum.TinhTrangPhong.SanSang_1).ToString();
                            }
                        }
                        db.ChiTiet_ChiTietPhieuDatPhongs.DeleteAllOnSubmit(ctpd.ChiTiet_ChiTietPhieuDatPhongs.ToList());
                    }
                    db.ChiTietPhieuDatPhongs.DeleteAllOnSubmit(lstChiTietPhieuDat);
                    db.PhieuDatPhongs.DeleteOnSubmit(phieuDatPhong);
                    db.SubmitChanges();
                }
                Frm_BarButton_SapXepPhongDatTruoc_Load(null, null);
            }
            catch (Exception ex)
            {
                Log.WriteLog("Frm_BarButton_SapXepPhongDatTruoc:butHuy_Click " + ex.Message + "\n" + ex.StackTrace);
                MessageBox.Show("Có lỗi xảy ra!!!");
            }
        }
    }
}